In [1]:
 conda install Pillow
Retrieving notices: ...working... done
Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... done

## Package Plan ##

  environment location: C:\Users\22077\anaconda3

  added / updated specs:
    - pillow


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    openssl-1.1.1v             |       h2bbff1b_0         5.5 MB
    ------------------------------------------------------------
                                           Total:         5.5 MB

The following packages will be UPDATED:

  openssl                                 1.1.1u-h2bbff1b_0 --> 1.1.1v-h2bbff1b_0 



Downloading and Extracting Packages

openssl-1.1.1v       | 5.5 MB    |            |   0% 
openssl-1.1.1v       | 5.5 MB    |            |   0% 
openssl-1.1.1v       | 5.5 MB    |            |   1% 
openssl-1.1.1v       | 5.5 MB    |            |   1% 
openssl-1.1.1v       | 5.5 MB    | 1          |   2% 
openssl-1.1.1v       | 5.5 MB    | 2          |   2% 
openssl-1.1.1v       | 5.5 MB    | 3          |   3% 
openssl-1.1.1v       | 5.5 MB    | 3          |   4% 
openssl-1.1.1v       | 5.5 MB    | 4          |   4% 
openssl-1.1.1v       | 5.5 MB    | 5          |   5% 
openssl-1.1.1v       | 5.5 MB    | 6          |   7% 
openssl-1.1.1v       | 5.5 MB    | 7          |   7% 
openssl-1.1.1v       | 5.5 MB    | 8          |   9% 
openssl-1.1.1v       | 5.5 MB    | 9          |   9% 
openssl-1.1.1v       | 5.5 MB    | #1         |  11% 
openssl-1.1.1v       | 5.5 MB    | #2         |  12% 
openssl-1.1.1v       | 5.5 MB    | #3         |  13% 
openssl-1.1.1v       | 5.5 MB    | #5         |  16% 
openssl-1.1.1v       | 5.5 MB    | #6         |  16% 
openssl-1.1.1v       | 5.5 MB    | #7         |  17% 
openssl-1.1.1v       | 5.5 MB    | #9         |  20% 
openssl-1.1.1v       | 5.5 MB    | ##         |  21% 
openssl-1.1.1v       | 5.5 MB    | ##2        |  22% 
openssl-1.1.1v       | 5.5 MB    | ##4        |  25% 
openssl-1.1.1v       | 5.5 MB    | ##5        |  26% 
openssl-1.1.1v       | 5.5 MB    | ##6        |  27% 
openssl-1.1.1v       | 5.5 MB    | ##8        |  28% 
openssl-1.1.1v       | 5.5 MB    | ##9        |  29% 
openssl-1.1.1v       | 5.5 MB    | ###        |  30% 
openssl-1.1.1v       | 5.5 MB    | ###1       |  32% 
openssl-1.1.1v       | 5.5 MB    | ###4       |  34% 
openssl-1.1.1v       | 5.5 MB    | ###5       |  35% 
openssl-1.1.1v       | 5.5 MB    | ###6       |  37% 
openssl-1.1.1v       | 5.5 MB    | ###7       |  38% 
openssl-1.1.1v       | 5.5 MB    | ###8       |  39% 
openssl-1.1.1v       | 5.5 MB    | ####       |  41% 
openssl-1.1.1v       | 5.5 MB    | ####1      |  42% 
openssl-1.1.1v       | 5.5 MB    | ####2      |  43% 
openssl-1.1.1v       | 5.5 MB    | ####4      |  44% 
openssl-1.1.1v       | 5.5 MB    | ####5      |  46% 
openssl-1.1.1v       | 5.5 MB    | ####7      |  47% 
openssl-1.1.1v       | 5.5 MB    | ####9      |  49% 
openssl-1.1.1v       | 5.5 MB    | #####      |  51% 
openssl-1.1.1v       | 5.5 MB    | #####2     |  52% 
openssl-1.1.1v       | 5.5 MB    | #####3     |  54% 
openssl-1.1.1v       | 5.5 MB    | #####5     |  55% 
openssl-1.1.1v       | 5.5 MB    | #####6     |  57% 
openssl-1.1.1v       | 5.5 MB    | #####8     |  58% 
openssl-1.1.1v       | 5.5 MB    | ######     |  60% 
openssl-1.1.1v       | 5.5 MB    | ######1    |  62% 
openssl-1.1.1v       | 5.5 MB    | ######2    |  63% 
openssl-1.1.1v       | 5.5 MB    | ######4    |  65% 
openssl-1.1.1v       | 5.5 MB    | ######6    |  66% 
openssl-1.1.1v       | 5.5 MB    | ######8    |  69% 
openssl-1.1.1v       | 5.5 MB    | #######    |  70% 
openssl-1.1.1v       | 5.5 MB    | #######2   |  72% 
openssl-1.1.1v       | 5.5 MB    | #######4   |  74% 
openssl-1.1.1v       | 5.5 MB    | #######6   |  76% 
openssl-1.1.1v       | 5.5 MB    | #######7   |  78% 
openssl-1.1.1v       | 5.5 MB    | #######9   |  79% 
openssl-1.1.1v       | 5.5 MB    | ########1  |  81% 
openssl-1.1.1v       | 5.5 MB    | ########3  |  83% 
openssl-1.1.1v       | 5.5 MB    | ########5  |  85% 
openssl-1.1.1v       | 5.5 MB    | ########6  |  87% 
openssl-1.1.1v       | 5.5 MB    | ########8  |  89% 
openssl-1.1.1v       | 5.5 MB    | #########  |  90% 
openssl-1.1.1v       | 5.5 MB    | #########2 |  92% 
openssl-1.1.1v       | 5.5 MB    | #########3 |  94% 
openssl-1.1.1v       | 5.5 MB    | #########6 |  96% 
openssl-1.1.1v       | 5.5 MB    | #########8 |  99% 
openssl-1.1.1v       | 5.5 MB    | ########## | 100% 
                                                     


Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done

Note: you may need to restart the kernel to use updated packages.

==> WARNING: A newer version of conda exists. <==
  current version: 23.3.1
  latest version: 23.7.2

Please update conda by running

    $ conda update -n base -c defaults conda

Or to minimize the number of packages updated during conda update use

     conda install conda=23.7.2


COMPUTER VISION¶

Computer vision is a field of artificial intelligence that gives computers the ability to see, understand, and interpret the visual world The goal of computer vision is to enable computers to see the world in the same way that humans do. This is a challenging task, as the visual world is complex and constantly changing. However, recent advances in artificial intelligence and machine learning have made significant progress in the field of computer vision

what is an Image

a digital image is not a continuous image, like a painting or a photograph. Instead, it is made up of a grid of pixels, and each pixel has a specific value that represents its color or brightness. It is an array of pixels arranged in columns and rows. Pixels are the elements of an image that contain information about intensity and color.Pixels are arranged in the form of a matrix. This is known as an RGB image

what is Image Processing ?

Image Processing is the core part of computer vision. It plays a crucial role in many real-world examples like robotics, self-driving cars, and object detection. Image processing allows us to transform and manipulate thousands of images at a time and extract useful insights from them. It has a wide range of applications in almost every field.

Why Python ?

Python is one of the widely used programming languages for this purpose. Its amazing libraries and tools help in achieving the task of image processing very efficiently.

what we are going to be exploring today ?

  • image processing using Python Numpy, Pillow and OpenCV
  • working with image data as structured array
In [ ]:
 
In [ ]:
 

RESOURCES¶

  • what is a digital image?
  • Real Python image processing
  • Open CV documentation
  • Pillow Documentation
  • Image processing
In [2]:
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
In [4]:
# Import images

pic = plt.imread(r"C:\Users\22077\Downloads\Covenant.jpg") #to read the color pixels
pic
Out[4]:
array([[[201, 215, 218],
        [199, 213, 214],
        [198, 212, 213],
        ...,
        [134, 109, 105],
        [124, 109, 102],
        [129, 119, 110]],

       [[199, 213, 214],
        [198, 212, 213],
        [198, 212, 213],
        ...,
        [138, 113, 109],
        [128, 109, 105],
        [133, 115, 111]],

       [[197, 211, 212],
        [198, 212, 213],
        [200, 214, 215],
        ...,
        [136, 110, 109],
        [132, 106, 105],
        [137, 111, 110]],

       ...,

       [[112, 135,  83],
        [111, 134,  82],
        [111, 134,  82],
        ...,
        [112, 137,  79],
        [122, 147,  89],
        [118, 143,  85]],

       [[114, 137,  85],
        [110, 133,  81],
        [109, 132,  80],
        ...,
        [113, 138,  80],
        [116, 141,  83],
        [116, 141,  83]],

       [[112, 135,  83],
        [108, 131,  79],
        [107, 130,  78],
        ...,
        [105, 130,  72],
        [115, 140,  82],
        [106, 131,  73]]], dtype=uint8)
In [5]:
#importing images 
#load images using numpy 

pic = plt.imread(r"C:\Users\22077\Downloads\Covenant.jpg") #to read the color pixels
plt.imshow(pic)
Out[5]:
<matplotlib.image.AxesImage at 0x264c01ce1a0>
In [6]:
# filtering the images with colour gradient
# RGB
# R(0) B(1) G(2)

red = pic[:,:,0]
red
Out[6]:
array([[201, 199, 198, ..., 134, 124, 129],
       [199, 198, 198, ..., 138, 128, 133],
       [197, 198, 200, ..., 136, 132, 137],
       ...,
       [112, 111, 111, ..., 112, 122, 118],
       [114, 110, 109, ..., 113, 116, 116],
       [112, 108, 107, ..., 105, 115, 106]], dtype=uint8)
In [7]:
plt.imshow(red)
Out[7]:
<matplotlib.image.AxesImage at 0x264c028b490>
In [8]:
# filtering the images with colour gradient
# RGB
# R(0) B(1) G(2)

green = pic[:,:,1]
green
Out[8]:
array([[215, 213, 212, ..., 109, 109, 119],
       [213, 212, 212, ..., 113, 109, 115],
       [211, 212, 214, ..., 110, 106, 111],
       ...,
       [135, 134, 134, ..., 137, 147, 143],
       [137, 133, 132, ..., 138, 141, 141],
       [135, 131, 130, ..., 130, 140, 131]], dtype=uint8)
In [9]:
plt.imshow(green)
Out[9]:
<matplotlib.image.AxesImage at 0x264c64ff640>
In [10]:
# filtering the images with colour gradient
# RGB
# R(0) B(1) G(2)

blue = pic[:,:,2]
blue
Out[10]:
array([[218, 214, 213, ..., 105, 102, 110],
       [214, 213, 213, ..., 109, 105, 111],
       [212, 213, 215, ..., 109, 105, 110],
       ...,
       [ 83,  82,  82, ...,  79,  89,  85],
       [ 85,  81,  80, ...,  80,  83,  83],
       [ 83,  79,  78, ...,  72,  82,  73]], dtype=uint8)
In [11]:
plt.imshow(blue)
Out[11]:
<matplotlib.image.AxesImage at 0x264c6563d30>
In [12]:
# Creating Canvas using Numpy 

cav = np.zeros((900,900))

plt.imshow(cav)
Out[12]:
<matplotlib.image.AxesImage at 0x264c74f4430>
In [32]:
# loading images using pill library
pic1 = Image.open(r"C:\Users\22077\Downloads\CR7.jpeg")
#pic1 = pic1.resize((500,500))
pic1
Out[32]:
In [37]:
red_pic,green_pic, blue_pic = pic1.copy(),pic1.copy(),pic1.copy()
In [36]:
pic1 = np.array(pic1)
pic1
Out[36]:
array([[[107, 156,  65],
        [107, 156,  65],
        [107, 156,  65],
        ...,
        [100, 149,  58],
        [100, 149,  58],
        [ 98, 147,  56]],

       [[107, 156,  65],
        [107, 156,  65],
        [107, 156,  65],
        ...,
        [100, 149,  58],
        [101, 150,  59],
        [ 98, 147,  56]],

       [[107, 156,  65],
        [107, 156,  65],
        [106, 155,  64],
        ...,
        [101, 150,  59],
        [101, 150,  59],
        [ 99, 148,  57]],

       ...,

       [[130,  15,  12],
        [126,  10,  10],
        [131,  13,  13],
        ...,
        [153,  19,  30],
        [154,  18,  30],
        [152,  14,  27]],

       [[129,  16,  12],
        [125,  11,  10],
        [129,  13,  13],
        ...,
        [154,  18,  30],
        [155,  17,  30],
        [152,  14,  27]],

       [[129,  16,  12],
        [125,  11,  10],
        [129,  13,  13],
        ...,
        [154,  18,  30],
        [155,  17,  30],
        [154,  14,  27]]], dtype=uint8)
In [39]:
pic1 = np.array(pic1)
red_pic[:,:,(1,2)] = 0
plt.imshow(red_pic)
Out[39]:
<matplotlib.image.AxesImage at 0x264c1a00460>
In [40]:
img_red = np.array(red_pic)
plt.imshow(img_red[:,:,(1,2)] = 0) # this is showing red. meaning 
  Cell In[40], line 2
    plt.imshow(img_red[:,:,(1,2)] = 0) # this is showing red. meaning
               ^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
In [ ]:
# Assuming pic1 is your original image
red_pic, green_pic, blue_pic = pic1.copy(), pic1.copy(), pic1.copy()

red_pic[:, :, (1, 2)] = 0
green_pic[:, :, (0, 2)] = 0
blue_pic[:, :, (0, 1)] = 0

cmb = np.concatenate((red_pic, green_pic, blue_pic), axis=1)
plt.imshow(cmb)
plt.show()
In [41]:
pic1 = np.array(pic)
red_pic,green_pic,blue_pic = pic1.copy(),pic1.copy(),pic1.copy()

red_pic[:,:,(1,2)] = 0
green_pic[:,:,(0,2)] = 0
blue_pic[:,:,(0,1)]= 0

cmb = np.concatenate((red_pic,green_pic,blue_pic),axis=1)
plt.figure(figsize=(11,11))
plt.imshow(cmb)
Out[41]:
<matplotlib.image.AxesImage at 0x264d630b2e0>
In [63]:
output_file = "cmb.jpg"  # Replace this with your desired output file name and format
plt.savefig("cmb.jpg")
<Figure size 640x480 with 0 Axes>
In [42]:
red_pic,green_pic,blue_pic = pic1.copy(),pic1.copy(),pic1.copy()

red_pic[:,:,(1,2)] = 0
green_pic[:,:,(0,2)] = 0
blue_pic[:,:,(0,1)]= 0

cmb = np.concatenate((red_pic,green_pic,blue_pic),axis=1)
plt.figure(figsize=(11,11))
plt.imshow(cmb)
Out[42]:
<matplotlib.image.AxesImage at 0x264d62cfa00>
In [44]:
#Gray Scale image 

gray = pic[:,:,0] 
plt.imshow(gray, cmap='gray')
Out[44]:
<matplotlib.image.AxesImage at 0x264cddff6a0>
In [45]:
# open CV library 
# it uses the BRG format rather than RBG like the Pillow or Numpy
import cv2
In [47]:
cv_image = cv2.imread(r"C:\Users\22077\Downloads\Covenant.jpg")

plt.imshow(cv_image) # note the blue image because the openCV library has the BRG format. Hence, more blue
Out[47]:
<matplotlib.image.AxesImage at 0x264cdf06c80>
In [49]:
#to have the original colour

cv_image2 = cv2.cvtColor(cv_image,cv2.COLOR_BGR2RGB) # Note the BRG2RGB. we can also do BRG2GRAY
plt.imshow(cv_image2)
Out[49]:
<matplotlib.image.AxesImage at 0x264cdfdc220>
In [54]:
cv_image2 = cv2.cvtColor(cv_image,cv2.COLOR_BGR2GRAY) # Note the BRG2RGB. we can also do BRG2GRAY
plt.imshow(cv_image2)
Out[54]:
<matplotlib.image.AxesImage at 0x264d1c06260>
In [51]:
flip = cv2.flip(cv_image2, -1) #vertical flip
plt.imshow(flip)
Out[51]:
<matplotlib.image.AxesImage at 0x264ce0949d0>
In [52]:
flip = cv2.flip(cv_image, 1) #vertical flip
plt.imshow(flip) #look closely to see the flip from right to left 
Out[52]:
<matplotlib.image.AxesImage at 0x264ce0f8fd0>
In [57]:
# Rotating images in openCV

img = Image.open(r"C:\Users\22077\Downloads\Covenant.jpg")
rot_im = img.rotate(45) # rotation
plt.figure(figsize=(10,10))
plt.imshow(rot_im)
Out[57]:
<matplotlib.image.AxesImage at 0x264d1d7ab60>
In [59]:
# Creating Canvas using Numpy 
# Creating Graphical images

cav1 = np.zeros((900,900,3))# the '3' will change it to 

plt.imshow(cav1)
Out[59]:
<matplotlib.image.AxesImage at 0x264d1b3e290>
In [69]:
# Creating Canvas using Numpy 
# Creating Graphical images

cav2 = np.zeros((900,900,3))# the '3' will change it to black

plt.imshow(cav2)
Out[69]:
<matplotlib.image.AxesImage at 0x264fb323040>
In [70]:
cv2.rectangle(cav2,pt1=[200,200],pt2=[800,800],color=(60,150,255),thickness=8)
plt.imshow(cav2)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Out[70]:
<matplotlib.image.AxesImage at 0x264fb3b5420>
In [75]:
cav4 = np.ones((1000,1000,1))
plt.imshow(cav4)
Out[75]:
<matplotlib.image.AxesImage at 0x264847ec9d0>
In [76]:
cv2.circle(cav4, center=(500,650),radius=20, color=(0,150,240), thickness=5)
plt.imshow(cav4)
Out[76]:
<matplotlib.image.AxesImage at 0x2648483b490>
In [77]:
can5 = np.zeros((1000, 1000, 3), dtype=np.uint8)

red = can5.copy()
blue = can5.copy()
green = can5.copy()

# Set the value for the colored squares in each channel
red[200:400, 200:400, 0] = 255  # R channel (Red square)
blue[250:450, 250:450, 2] = 255  # B channel (Blue square)
green[300:500, 300:500, 1] = 255  # G channel (Green square)

# Convert the colored squares to grayscale
red = Image.fromarray(red).convert("L")
blue = Image.fromarray(blue).convert("L")
green = Image.fromarray(green).convert("L")

# Merge the grayscale images back into a single RGB image
C = Image.merge("RGB", [red, green, blue])

# Display the merged image
plt.imshow(C)
plt.show()
In [ ]: